//
//  XiaoKaiUserFirstCollectionView.m
//  Project_A
//
//  Created by 凯 on 15/7/21.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "XiaoKaiUserFirstCollectionView.h"
#import "XiaoKaiUserCollectionViewCell.h"
#import "BaseHeader.h"
#import "DataBase.h"


@interface XiaoKaiUserFirstCollectionView ()
@property (nonatomic, retain) XiaoKaiUserCollectionViewCell *copyCell;

@end
@implementation XiaoKaiUserFirstCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.Hight = YES;
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[XiaoKaiUserCollectionViewCell class] forCellWithReuseIdentifier:@"userfirstcollectionviewcell"];
        //注册头部区域
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XiaoKaiUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"userfirstcollectionviewcell" forIndexPath:indexPath];
    self.copyCell = cell;
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //通过判断kind参数类型 确定是头部区域还是尾部区域
    if (kind == UICollectionElementKindSectionHeader) {
        //从头部区域 重用池中获取f头部对象
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor whiteColor];
        [header addSubview:_myView];
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.copyCell.collection.contentOffset.x < SCREEN_WIDTH) {
        DataBase *dbManger = [DataBase shareInstance];
        [dbManger openDB];
        NSMutableArray *arr = [dbManger selectInfo];
        return CGSizeMake(SCREEN_WIDTH, arr.count * SCREEN_HEIGHT / 4);
    } else {
        DataBase *dbManger = [DataBase shareInstance];
        [dbManger openDB];
        NSMutableArray  *arr1 = [dbManger selectItemsInfo];
        [dbManger closeDB];
        if (arr1.count % 2 == 0) {
            return CGSizeMake(SCREEN_WIDTH, arr1.count / 2 * 205 + 20);
        } else {
            return CGSizeMake(SCREEN_WIDTH, arr1.count / 2 * 205 + 205 + 20);
        }
    }
}
- (void)reloadData
{
    [super reloadData];
    [self.copyCell.collection reloadData];
}


@end
